# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

k=3
root=TreeNode(5)
root.left=TreeNode(3)
root.left.left=TreeNode(2)
root.left.left.left=TreeNode(1)
root.right=TreeNode(6)
root.left.right=TreeNode(4)

def kthSmallest( root: TreeNode, k: int) -> int:
    stack = []
    while root or stack:
        while root:
            stack.append(root)
            root = root.left
        root = stack.pop()
        k -= 1
        if k == 0:
            return root.val
        root = root.right

print(kthSmallest(root,k))